// pages/learn/list.js
import {
  getLearnCenterList
} from '../../api/course.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    courseList: [],
    pageNo: 1,
    rowCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      isLogin: getApp().globalData.isLogin
    })

    if (getApp().globalData.isLogin) {
      this.data.pageNo = 1;
      this.data.courseList.length = 0;
      this.loadLearnCenterList();
    }
  },

  /**
   * 获取学习列表
   */
  loadLearnCenterList() {
    getLearnCenterList(this.data.pageNo).then(res => {
      console.log('学习中心',res)
      this.setData({
        courseList: this.data.courseList.concat(res.list),
        rowCount: res.rowCount,
      })
    })
  },

  /**
   * 跳转课程详情
   * @param {object} e 
   */
  navigatorCourse(e) {
    wx.navigateTo({
      url: '/pages/learn/course/details?isMine=1&id=' + e.detail,
    })
  },

  /**
   * 跳转授权登录
   */
  navigatorAccredit() {
    wx.navigateTo({
      url: '/pages/login/accredit/accredit',
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.rowCount > this.data.courseList.length) {
      this.data.pageNo += 1;
      this.loadLearnCenterList();
    }
  },

})